#!/bin/sh
set -e

# allow user to pass dist(s) on command-line
if [ $# -gt 0 ]; then
  dists=($@)
else
  dists=(10.2-gcc3.3 10.3 10.4 10.5)
fi

for dist in ${dists[@]}; do
  # 10.5 uses the 10.4 dir
  if [ $dist = "10.5" ]; then
    dists_dir=( "${dists_dir[@]}" "10.4" )
  else
    dists_dir=( "${dists_dir[@]}" $dist )
  fi
done

if [ -z "$SKIP_UPDATE" ]; then
  echo ""
  if [ -d fink ] && [ -d /var/www/finkinfo/dists.public ]; then
    echo "Updating fink/ via local rsync..."
    rsync --delete -avr /var/www/finkinfo/dists.public/ fink/
  else
    if [ -d fink/CVS ]; then
      echo "Updating ${dists_dir[@]} from CVS..."
      cd fink
      cvs -z3 -q update -Al
      cvs -z3 -q update -Ad ${dists_dir[@]}
      cd ..
    else
      echo "Checking out ${dists_dir[@]} from CVS..."
      cvs -z3 -d:pserver:anonymous@fink.cvs.sourceforge.net:/cvsroot/fink co -d fink dists
    fi
  fi
fi

# flush the previous dump
echo -n "" > current_pdb_dump.sql

# Dump each supported source tree of the dists
dist_no=0
for dist in ${dists[@]}; do
    rm -f fink/dists var/db/fink.db
	echo ""
    echo "Linking 'fink/dists' to '${dists_dir[$dist_no]}'..."
	echo ""
    ln -s ${dists_dir[$dist_no]} fink/dists
    for release in stable unstable; do
	if [ $dist = "10.4" ] || [ $dist = "10.5" ]; then
	    for arch in powerpc i386; do
	      echo ""
	      echo "Dumping $dist/$release $arch..."
	      ./dump --distribution=$dist --release=$release --architecture=$arch --lucene --indexpath=`pwd`/lucene-index >> current_pdb_dump.sql
	      sleep 60
	    done
	else
	    echo ""
	    echo "Dumping $dist/$release..."
	    ./dump --distribution=$dist --release=$release --architecture=powerpc --lucene --indexpath=`pwd`/lucene-index >> current_pdb_dump.sql
	    sleep 60
	fi
    done
    dist_no=`expr $dist_no + 1`
done

echo ""

exit 0
